<?php
$this->breadcrumbs = array(
    '技能等级' => array('admin'),
    '管理',
);
Yii::app()->clientScript->registerScript('search', "
$('form.search-form').submit(function(){
	$.fn.yiiGridView.update('product-skill-level-grid', {
		data: $(this).serialize()
	});
	return false;
});
$('#delete-button').click(function(){
    var selects = $.fn.yiiGridView.getSelection('product-skill-level-grid');
    if(selects==''){
        alert('请选中记录进行操作！');
    }else{
       if(window.confirm('您确定要删除吗？')){
            $.fn.yiiGridView.update('product-skill-level-grid', {
		type:'POST',
		url:'" . $this->rewriteCreateUrl('delete') . "',
                                    data:{id:selects},
		success:function(data) {
                    $.fn.yiiGridView.update('product-skill-level-grid');
                    //afterDelete(th,true,data);
		},
		error:function() {
                    //afterDelete(th,false);
		}
            });
            return false;
       }
    }
});
");
?>
<div class="box corners shadow">
    <div class="box-header">
        <h2>技能等级管理</h2>
        <div class="box-header-ctrls">	
            <a href="javascript:void(null);" title="" class="close"><!-- --></a>
        </div>
    </div>

    <div class="box-content" id="contacts-1a">
        <div class="inbox-sf2">
            <form class="search-form">
                <?php
                echo CHtml::dropDownList('game', '', array('' => '') + ProductCategory::model()->getGameSelect(), array('class' => 'tip', 'title' => '选择游戏', 'onchange' => 'getSkill(this, "ProductSkillLevel_skill_id")'));
                echo CHtml::dropDownList('ProductSkillLevel[skill_id]', '', array('' => '--'), array('class' => 'tip', 'title' => '技能'));
                ?>
                <input type="submit" name="" value="查询" class="inbox-sf-search-btn" />
            </form>
            <a class="button black fr tip" id="delete-button" href="javascript:void(null);" title="选中数据进行删除！">
                <span>批量删除</span>
            </a>
            <a class="button black fr" href="<?php echo $this->rewriteCreateUrl('create') ?>">
                <span>添加等级</span>
            </a>
            <a class="button red fr" href="javascript:void(0)" onclick="$('#quick-edit').toggle()">
                <span>批量添加/修改</span>
            </a>
        </div> 
        <div class="inbox-sf2" id="quick-edit" style="display:none;">
            <form class="update-form">
                <?php
                echo CHtml::dropDownList('update_game', '', array('' => '') + ProductCategory::model()->getGameSelect(), array('class' => 'tip', 'title' => '选择游戏', 'onchange' => 'getSkill(this, "skill_id")'));
                echo CHtml::dropDownList('skill_id', '', array('' => '--'), array('class' => 'tip', 'title' => '技能'));
                ?>
                <input rel-message="开始等级格式错误" rel-type="number" name="skill_slevel" id="skill_slevel" rel-default="开始等级" type="text" class="input-1 tip" title="开始等级" value="开始等级" />
                <input rel-message="结束等级格式错误" rel-type="number" name="skill_elevel" id="skill_elevel" rel-default="结束等级" type="text" class="input-1 tip" title="结束等级" value="结束等级" />
                <input rel-message="时间必须填写" rel-type="number" name="skill_time" id="skill_time" rel-default="时间(小时)" type="text" class="input-1 tip" title="时间(小时)" value="时间(小时)" />
                <input rel-message="价格必须填写" rel-type="number" name="skill_price" id="skill_price" rel-default="价格($)" type="text" class="input-1 tip" title="价格($)" value="价格($)" />
                <input type="submit" name="" value="提交" class="inbox-sf-search-btn" />
            </form>
        </div>
        <?php
        $this->widget('zii.widgets.grid.CGridView', array(
            'id' => 'product-skill-level-grid',
            'dataProvider' => $model->search(),
            'cssFile' => '',
            //'ajaxUpdate' => false,
            'htmlOptions' => array('class' => ''),
            'template' => '<div id="loading"></div>{summary}{items}{pager}',
            'itemsCssClass' => 'tablebox',
            'pager' => array(//通过pager设置样式   默认为CLinkPager
                'prevPageLabel' => '<<',
                'firstPageLabel' => '首页', //first,last 在默认样式中为{display:none}及不显示，通过样式{display:inline}即可
                'nextPageLabel' => '>>',
                'lastPageLabel' => '末页',
                'header' => '',
                'htmlOptions' => array('class' => 'box-nav')
            ),
            'emptyText' => '<font size="4" color="red">请设置条件进行查询！</font>',
            'selectableRows' => 2,
            'columns' => array(
                array(
                    'class' => 'CCheckBoxColumn',
                    'header' => '全选',
                    'value' => '$data->id',
                    'id' => 'delete',
                    'htmlOptions' => array('style' => 'width:20px')
                ),
                'id',
                array(
                    'name' => 'skill_id',
                    'value' => 'ProductSkill::model()->getFieldById($data->skill_id)',
                ),
                'skill_level',
                array(
                    'name' => 'skill_time',
                    'type' => 'raw',
                    'value' => 'ProductSkillLevel::model()->createInput("skill_time", $data->id, $data->skill_time)',
                ),
                array(
                    'name' => 'skill_price',
                    'type' => 'raw',
                    'value' => 'ProductSkillLevel::model()->createInput("skill_price", $data->id, $data->skill_price)',
                ),
                array(
                    'name' => 'skill_gold',
                    'type' => 'raw',
                    'value' => 'ProductSkillLevel::model()->createInput("skill_gold", $data->id, $data->skill_gold)'
                ),
                'update_time',
                array(
                    'type' => 'raw',
                    'htmlOptions' => array('style' => 'width:140px'),
                    'value' => 'Yii::app()->controller->getColumnOptions($data->id,"ProductSkillLevel")',
                ),
            ),
        ));
        ?>         
    </div><!-- END ".box-content" --> 
</div>
<script>
    $(function(){
        
        $("input.input-1").focus(function(){
            if($(this).val() == $(this).attr("rel-default")){
                $(this).val("");
            }
        }).blur(function(){
            if($(this).val() == ""){
                $(this).val($(this).attr("rel-default"));
            }
        });
        
        $('form.update-form').submit(function(){//批量更新数据
            if($("#skill_id").val() == ""){
                alert("请选择技能");
                return false;
            }
            var message="";
            $.each($(this).find("input.input-1"), function(i, n){
                switch($(n).attr("rel-type")){
                    case 'number':
                        if(!isNumber($(n).val())){
                            message += $(n).attr("rel-message")+"\n";
                        }
                        break;
                }
            });
            if(message == ""){
                $.ajax({
                    type: "POST",
                    url: "<?php echo $this->createUrl("ajax");?>",
                    data:$(this).serialize()+"&action=quickUpdate",
                    success:function(data){
                        alert(data);
                    }
                });
            }else{
                alert(message);
            }
            
            return false;
        });
        
        
    });

    
    function updateField(obj){//更新单个数据内容
        $(obj).addClass("loading");
        $.post("<?php echo $this->createUrl('ajax'); ?>", {field:$(obj).attr("rel-field"), id:$(obj).attr("rel-id"), fieldVal:$(obj).val(), action:"update"}, function(data){
            if(data){
                $(obj).removeClass("loading").addClass("ok");
            }else{
                $(obj).removeClass("loading").addClass("error");
            }
        });
    }

    function getSkill(obj, setId){
        $.post("<?php echo $this->createUrl('productSkill/ajax'); ?>", {game:$(obj).val()}, function(data){
            if(data){
                $("#"+setId).find("option").remove();
                $.each(data, function(i, n){
                    $("#"+setId).append('<option value="'+i+'">'+n+'</option>');
                });
            }
        }, "json");
    }
    
    function isNumber(n) {//判断变量是否为数字
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
</script>